/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.iecg.controller;

import com.aliyun.oss.model.SimplifiedObjectMeta;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iecg.api.entity.IecgVersion;
import com.iecg.service.IecgVersionService;
import com.pig4cloud.common.minio.service.MinioTemplate;
import com.pig4cloud.common.oss.service.OssTemplate;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 版本控制
 *
 * @author pigx code generator
 * @date 2019-12-24 15:09:16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/version" )
@Api(value = "iecgversion", tags = "版本控制管理")
public class IecgVersionController {

    private final IecgVersionService iecgVersionService;

	private final MinioTemplate minioTemplate;

	private final OssTemplate ossTemplate;

    /**
     * 分页查询
     * @param page 分页对象
     * @param iecgVersion 版本控制
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getIecgVersionPage(Page page, IecgVersion iecgVersion) {
        return R.ok(iecgVersionService.page(page, Wrappers.query(iecgVersion)));
    }


    /**
     * 通过id查询版本控制
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(iecgVersionService.getById(id));
    }

    /**
     * 新增版本控制
     * @param iecgVersion 版本控制
     * @return R
     */
    @ApiOperation(value = "新增版本控制", notes = "新增版本控制")
    @SysLog("新增版本控制" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('iecg_version_add')" )
    public R save(@RequestBody IecgVersion iecgVersion){
		String name = iecgVersion.getName();
		String bucketName = "iecg";

		// 列举文件。 如果不设置KeyPrefix，则列举存储空间下所有的文件。KeyPrefix，则列举包含指定前缀的文件。
		SimplifiedObjectMeta simplifiedObjectMeta = ossTemplate.getSimplifiedObjectMeta(bucketName, name);
		iecgVersion.setFileSize(simplifiedObjectMeta.getSize());
		iecgVersion.setPublishTime(simplifiedObjectMeta.getLastModified());
//		String downloadUrl = minioTemplate.getObjectURL(bucketName, objectName, 7* 24 * 3600);
//		String foreverDownloadUrl = downloadUrl.split("\\?")[0];
//		iecgVersion.setDownload(foreverDownloadUrl);
//		try {
//			ObjectStat objectStat = minioTemplate.getObjectInfo(bucketName, objectName);
//			iecgVersion.setPublishTime(objectStat.createdTime());
//			iecgVersion.setFileSize(objectStat.length());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		return R.ok(iecgVersionService.save(iecgVersion));
    }

    /**
     * 修改版本控制
     * @param iecgVersion 版本控制
     * @return R
     */
    @ApiOperation(value = "修改版本控制", notes = "修改版本控制")
    @SysLog("修改版本控制" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('iecg_version_edit')" )
    public R updateById(@RequestBody IecgVersion iecgVersion) {
        return R.ok(iecgVersionService.updateById(iecgVersion));
    }

    /**
     * 通过id删除版本控制
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除版本控制", notes = "通过id删除版本控制")
    @SysLog("通过id删除版本控制" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('iecg_version_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(iecgVersionService.removeById(id));
    }

}
